// pages/sort/sort.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';

var loginFlag=false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '合肥市',
    sortList: [], //获取分类信息
    sortListIndex: 0, //获取分类的索引 默认为第一个
    business: ['人气商家', '附近商家'], //人气和附近
    businessIndex: 0, //人气和附近索引值
    page: 1, //店铺分页
    size: 10, //店铺分页展示个数
    totalPages: 0, //展示总分页数
    shopList: [], //店铺列表
    isShopList: true, //店铺列表是否有  没有会有提示信息
    isSearch: false, //是否点击搜索店铺信息
    searchText: '', //搜索信息
    searchShopList: [], //
    isSearchList: false, //搜索信息是否有数据
    istemplate: false,
    refreshFlag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    //console.log(options.index)
    if (options.index != undefined) {
      _this.setData({
        sortListIndex: options.index,
      })
    } else {
      _this.setData({
        sortListIndex: 0,
      })
    }
    this.setData({
      city: wx.getStorageSync('originalData').addressComponent.city
    })
    _this.loadIndexSort(); //分类店铺
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.isProductRelease();
    if (this.data.city !== wx.getStorageSync('originalData').addressComponent.city) {
      let _this = this;
      _this.setData({
        page: 1,
        sortListIndex: 0,
        businessIndex: 0,
        sortList: [],
        shopList: [],
        refreshFlag: true,
        city: wx.getStorageSync('originalData').addressComponent.city
      })
      wx.showNavigationBarLoading();
      _this.loadIndexSort(); //分类店铺
      setTimeout(() => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        _this.setData({
          refreshFlag: false
        });
      }, '2000')
    }
    
    if (app.globalData.loginFlag && !app.globalData.shoppingDataLength) {
      app.loadCartNum();
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (this.data.refreshFlag) {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.setData({
        refreshFlag: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (!this.data.refreshFlag) {
      //console.log("上拉刷新")
      let _this = this;
      _this.setData({
        page: 1,
        sortListIndex: 0,
        businessIndex: 0,
        sortList: [],
        shopList: [],
        refreshFlag: true
      })
      wx.showNavigationBarLoading();
      _this.loadIndexSort(); //分类店铺
      setTimeout(() => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        _this.setData({
          refreshFlag: false
        });
      }, '2000')
    } else {
      console.log('频繁刷新');
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    //console.log("下拉加载")
    let _this = this;
    if (_this.data.page < _this.data.totalPages) {
      _this.setData({
        page: _this.data.page + 1,
      })
      wx.showLoading({
        title: '加载中',
      })
      if (_this.data.businessIndex == 0) { //人气
        _this.getPopularityShop(function() {
          setTimeout(function() {
            wx.hideLoading()
          }, 1000)
        });
      } else { //附近
        _this.getNearbyShop(function() {
          setTimeout(function() {
            wx.hideLoading()
          }, 1000)
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    app.globalData.otherPageHideFlag = true;
  },
  //获取分类栏目信息
  loadIndexSort() {
    let _this = this;
    let data = {
      "city": wx.getStorageSync('originalData').addressComponent.city,
      "delFlag": false, //默认false
    }
    //console.log(data)
    Api.loadIndexSort(data).then(res => {
      console.log(res.data)
      _this.setData({
        sortList: res.data,
      })
      if (_this.data.sortList.length>0){
      if (_this.data.businessIndex == 0) { //人气
        _this.getPopularityShop();
      } else { //附近
        _this.getNearbyShop()
      }
        _this.setData({
          isShopList: true,
        })
      }else{
        _this.setData({
          isShopList: false,
        })
      }
    })
  },

  //分类切换
  changeSort(e) {
    let _this = this;
    let index = e.currentTarget.dataset.index;
    _this.setData({
      sortListIndex: index,
      page: 1,
      shopList: [],
    })
    if (_this.data.businessIndex == 0) { //人气
      _this.getPopularityShop();
    } else { //附近
      _this.getNearbyShop()
    }
  },

  //人气和附近商家切换
  changeBusiness(e) {
    let _this = this;
    let index = e.currentTarget.dataset.index;
    _this.setData({
      businessIndex: index,
      page: 1,
      shopList: [],
    })
    if (_this.data.businessIndex == 0) { //人气
      _this.getPopularityShop();
    } else { //附近
      _this.getNearbyShop()
    }
  },

  //获取人气商家店铺
  getPopularityShop(callback) {
    let _this = this;
    let data = {
      "categoryId": _this.data.sortList[_this.data.sortListIndex].id,
      "point": {
        "longitude": wx.getStorageSync('originalData').location.lng,
        "latitude": wx.getStorageSync('originalData').location.lat
      }
    }
    //console.log(data)
    Api.getPopularityShop(data, _this.data.page, _this.data.size).then(res => {
      console.log("人气商家", res.data.dataList)
      _this.setData({
        shopList: _this.data.shopList.concat(res.data.dataList),
        totalPages: res.data.totalPages,
        sortListIndex: _this.data.sortListIndex
      });
      if (_this.data.shopList.length > 0) {
        _this.setData({
          isShopList: true,
        })
      } else {
        _this.setData({
          isShopList: false,
        })
      }
      (callback && typeof(callback) === "function") && callback();
    })
  },

  //获取附近商家店铺
  getNearbyShop(callback) {
    let _this = this;
    let data = {
      "categoryId": _this.data.sortList[_this.data.sortListIndex].id,
      "point": {
        "longitude": wx.getStorageSync('originalData').location.lng,
        "latitude": wx.getStorageSync('originalData').location.lat
      }
    }
    //console.log(data)
    Api.getNearbyShop(data, _this.data.page, _this.data.size).then(res => {
      //console.log("附近商家", res)
      _this.setData({
        shopList: _this.data.shopList.concat(res.data.dataList),
        totalPages: res.data.totalPages,
        sortListIndex: _this.data.sortListIndex
      });
      if (_this.data.shopList.length > 0) {
        _this.setData({
          isShopList: true,
        })
      } else {
        _this.setData({
          isShopList: false,
        })
      }
      (callback && typeof(callback) === "function") && callback();
    })
  },

  //搜索店铺信息
  searchClick() {
    //console.log("搜索事件");
    let _this = this;
    _this.setData({
      isSearch: true,
    })
    let data = {
      "city": wx.getStorageSync('originalData').addressComponent.city,
      "shopName": _this.data.searchText,
      "longitude": wx.getStorageSync('originalData').location.lng,
      "latitude": wx.getStorageSync('originalData').location.lat
    }
    Api.getSearchSort(data).then(res => {
      console.log(res)
      _this.setData({
        searchShopList: res.data.dataList
      })
      if (res.data.dataList.length > 0) {
        _this.setData({
          isSearchList: true,
        })
      } else {
        _this.setData({
          isSearchList: false,
        })
      }
    })

  },

  //清除列表事件 取消搜索信息  返回分类列表数据
  colseSearchClick() {
    let _this = this;
    _this.setData({
      isSearch: false,
    })
  },

  //搜索输入的信息
  getShopText(e) {
    // console.log(e)
    let _this = this;
    _this.setData({
      searchText: e.detail.value,
    })
  },
  //判断产品发布信息
  isProductRelease() {
    let _this = this;
    Api.isProductRelease().then(res => {
      console.log('进入标记查询', res)
      _this.setData({
        istemplate: res.data
      })
    }).catch(err => {
      _this.setData({
        istemplate: true
      })
    });
  }

})